package com.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 消息发送控制器
 * @author wuyunbin
 */
@RestController
@Slf4j
@RequestMapping("ttl")
public class DelayedQueueController {
    public RabbitTemplate rabbitTemplate;

    @Autowired
    public DelayedQueueController(RabbitTemplate rabbitTemplate){
        this.rabbitTemplate=rabbitTemplate;
    }

    @GetMapping("/send/{message}/{ttl}")
    public void sendMessage(@PathVariable String message, @PathVariable Long ttl) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
        log.info("当前时间：{} 发送一条信息给队列{},延时时间{}秒",format.format(new Date()) , message, ttl/1000.0);

        rabbitTemplate.convertAndSend("test",message,(msg -> {
            //发送消息 并设置TTL
            msg.getMessageProperties().setExpiration(String.valueOf(ttl));
            return msg;
        }));

    }



}
